package com.app.system.utils.dataType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by wcf-pc on 2018/4/17.
 *
 * @author wcf-pc
 */
public class DateUtils {

    /**
     * 字符串转日期格式
     *
     * @param dateStr
     */
    public static Date stringToDateDefault(String dateStr) {
        return stringToDateFormat(dateStr, "yyyy-MM-dd");
    }

    /**
     * 日期转字符串格式
     *
     * @param date
     */
    public static String dateToStringDefault(Date date) {
        return dateToStringFormat(date, "yyyy-MM-dd");
    }
    /**
     * 日期转字符串格式
     *
     * @param date
     */
    public static String dateToStringDefault1(Date date) {
        return dateToStringFormat(date, "yyyy-MM-dd HH:mm:ss");
    }
    /**
     * 日期格式化输出（字符串）
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToStringFormat(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 日期格式化输出（字符串）
     * long类型转换
     *
     * @param dateLong
     * @param pattern
     * @return
     */
    public static String dateToStringFormat(Long dateLong, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date(dateLong));
    }

    /**
     * 日期格式化输出（日期）
     *
     * @param dateString
     * @param pattern
     * @return
     */
    public static Date stringToDateFormat(String dateString, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(dateString);
        } catch (ParseException e) {
        }
        return null;
    }

    //二个日期相差天数
    public static int getDaysByDateAndDate(Date startDate, Date stopDate) {
        return Math.abs((int) ((startDate.getTime() - stopDate.getTime()) / (1000 * 3600 * 24)));
    }

    //一个日期加几天等于新日期
    public static Date getDateByDateAndDays(Date startDate, int days) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(startDate);
            calendar.add(Calendar.DATE, days);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return calendar.getTime();

    }


    //一个日期加几年等于新日期
    public static Date getDateByDateAndYears(Date startDate, int years) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(startDate);
            calendar.add(Calendar.YEAR, years);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return calendar.getTime();

    }

    /**
     * 时间格式化输出（上或下）
     *
     * @param newDate
     * @return
     */
    public static String stringToDateTimeCompare(Date newDate) {
        if (newDate == null) {
            return null;
        }
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        try {
            date = format.parse("13:30");
            newDate = format.parse(DateUtils.dateToStringFormat(newDate, "HH:mm"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (newDate.getTime() < date.getTime()) {
            return "上午";
        } else if (newDate.getTime() > date.getTime()) {
            return "下午";
        } else {
            return null;
        }
    }
}
